package com.quiz.bis.controller;

import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.bis.domain.vo.StudentActivityVo;
import com.quiz.bis.domain.vo.StudentExamDetailVo;
import com.quiz.bis.service.IStudentActivityService;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 学生考试活动管理控制器
 */
@RestController
@RequestMapping("/student/exam")
@Tag(name = "学生考试活动管理")
@RequiredArgsConstructor
public class StudentActivityController extends BaseController {
    private final IStudentActivityService studentActivityService;

    /**
     * 分页查询学生考试活动列表
     *
     * @param groupId   考试组ID
     * @param type      活动类型(1-考试 2-测评 3-作业)
     * @param status    活动状态(1-未开始 2-进行中 3-已结束)
     * @param pageQuery 分页参数
     * @return 活动列表
     */
    @GetMapping("/list")
    public TableDataInfo<StudentActivityVo> list(@RequestParam Long groupId,
                                                 @RequestParam(required = false) Integer type,
                                                 @RequestParam(required = false) Integer status,
                                                 @RequestParam(required = false) String searchKey,
                                                 @Validated PageQuery pageQuery) {
        return studentActivityService.pageActivityList(groupId, type, status, searchKey, pageQuery);
    }

    /**
     * 获取活动详情
     *
     * @param activityId 活动ID
     * @return 活动详情
     */
    @GetMapping("/{activityId}")
    public R<StudentActivityVo> detail(@PathVariable Long activityId) {
        return R.ok(studentActivityService.getActivityDetail(activityId));
    }

    /**
     * 获取考试详情(包含试卷和题目信息)
     *
     * @param examId 考试ID
     * @return 考试详情
     */
    @GetMapping("/detail/{examId}")
    public R<StudentExamDetailVo> getExamDetail(@PathVariable Long examId) {
        return R.ok(studentActivityService.getExamDetail(examId));
    }

    /**
     * 保存考试作答草稿
     *
     * @param examId 考试ID
     * @param answersJson 答案JSON
     * @return 保存结果
     */
    @PostMapping("/saveDraft/{examId}")
    public R<Void> saveDraft(@PathVariable Long examId, @RequestBody String answersJson) {
        return toR(studentActivityService.saveExamDraft(examId, answersJson));
    }

    /**
     * 提交考试
     *
     * @param examId 考试ID
     * @param answersJson 答案JSON
     * @return 提交结果
     */
    @PostMapping("/submit/{examId}")
    public R<Void> submitExam(@PathVariable Long examId, @RequestBody String answersJson) {
        return toR(studentActivityService.submitExam(examId, answersJson));
    }
}